package e_锁机制;


import java.util.concurrent.locks.ReentrantLock;

public class TicketLock implements Runnable{
    private int ticket = 100;

    private ReentrantLock r = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            r.lock();
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    Thread t = Thread.currentThread();
                    String name = t.getName();
                    System.out.println(name + "正在卖票:" + ticket);
                    ticket--;
                } else {
                    break;
                }
            r.unlock();
        }
    }
}
